package com.zy.gmall.realtime.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.client.AsyncConnection;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {

    private static JedisPool jedisPool;
    static {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMinIdle(5);
        jedisPoolConfig.setMaxTotal(100);
        jedisPoolConfig.setMaxIdle(5);
        jedisPoolConfig.setBlockWhenExhausted(true);
        jedisPoolConfig.setMaxWaitMillis(2000L);
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPool = new JedisPool(jedisPoolConfig,"hadoop102",6379,10000);
    }
    //获取Jedis客户端
    public static Jedis getJedis(){
        System.out.println("~~~创建Jedis客户端~~~");
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }
    //关闭Jedis客户端
    public static void closeJedis(Jedis jedis){
        System.out.println("~~~关闭Jedis客户端~~~");
        if(jedis != null){
            jedis.close();
        }
    }


    public static StatefulRedisConnection<String,String> getAsyncRedisConnection(){
        //连接redis的0号库
        RedisClient redisClient = RedisClient.create("redis://hadoop102:6379/0");
        StatefulRedisConnection<String, String> conn = redisClient.connect();
        return conn;
    }

    //关闭支持异步操作的客户端
    public static void closeAsyncRedisConn(StatefulRedisConnection<String,String> redisConn){
        if(redisConn != null && redisConn.isOpen()){
            redisConn.close();
        }
    }

    //以异步的方式从Redis中获取数据
    public static JSONObject asyncGetDimInfo(StatefulRedisConnection<String,String> redisConn, String key){
        RedisAsyncCommands<String, String> asyncCommands = redisConn.async();
        try {
            String dimJsonStr = asyncCommands.get(key).get();
            if(StringUtils.isNotEmpty(dimJsonStr)){
                //缓存命中
                JSONObject dimJsonObj = JSON.parseObject(dimJsonStr);
                return dimJsonObj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 以异步的方式向Redis中写入数据
     * @param redisConn         异步连接
     * @param key               redis中的key
     * @param dimJsonObj               要写入的json对象
     */
    public static void asyncWriteDim(StatefulRedisConnection<String,String> redisConn, String key,
                                     JSONObject dimJsonObj){
        RedisAsyncCommands<String, String> asyncCommands = redisConn.async();
        asyncCommands.setex(key,3600*24,dimJsonObj.toJSONString());
    }


    public static void main(String[] args) {
        Jedis jedis = getJedis();
        String pong = jedis.ping();
        System.out.println(pong);
        closeJedis(jedis);
    }

}
